package leetCode;

import java.util.*;

public class SimplifyPath {
    class Node {
        Map<String, Node> childs = new HashMap<>();
        Node parent;

        public Node getChild(String p) {
            if (!this.childs.containsKey(p)) {
                this.childs.put(p, new Node());
            }
            return this.childs.get(p);
        }
    }

    public String simplifyPath(String path) {
        String[] paths = path.split("/");
        LinkedList<String> visited = new LinkedList<>();
        for (String p :
                paths) {
            if (p.isEmpty())
                continue;

            switch (p) {
                case ".":
//                cur=cur;

                    break;
                case "..":

                    if (!visited.isEmpty()) {
                        visited.removeLast();
                    }
                    break;
                default:
                    visited.add(p);
                    break;
            }
        }
        //杩斿洖浠巖oot鍒癱ur鐨勮矾寰勭殑鍊�
        StringBuilder stringBuilder = new StringBuilder();
        for (String p :
                visited) {
            stringBuilder.append("/");
            stringBuilder.append(p);
        }
        if (visited.isEmpty()) {
            stringBuilder.append("/");
        }
        return stringBuilder.toString();
    }

    private String trimMultiSlash(String path) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < path.length(); i++) {
            if (i != 0 && (path.charAt(i) == '/' && path.charAt(i - 1) == '/')) {
                continue;
            }
            stringBuilder.append(path.charAt(i));
        }
        return stringBuilder.toString();
    }

    public static void main(String[] arg) {
        String path = "/a//b////c/d//././/..";
        System.out.print(new SimplifyPath().simplifyPath(path));
    }
}
